package com.scs.application.modules.upms.controller;

import com.scs.application.core.model.CommonResult;
import com.scs.application.core.mvc.controller.BaseController;
import com.scs.application.modules.upms.dto.*;
import com.scs.application.modules.upms.service.LoginService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Description: 登录
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/upms")
public class LoginController extends BaseController {

    private final LoginService loginService;


    /**
     * 账号密码登录
     *
     * @param dto
     * @return
     */
    @GetMapping("/pwd_login")
    public CommonResult login(UsernameAndPasswordLoginDTO dto) {
        return CommonResult.ok("登录成功", loginService.login(dto));
    }


    /**
     * 微信登录
     *
     * @param dto
     * @return
     */
    @GetMapping("/wx_login")
    public CommonResult wxLogin(String oauth2Code,String loginKey,String openId) {
        return CommonResult.ok("登录成功", loginService.wxLogin(oauth2Code,loginKey,openId));
    }

    /**
     * 手机短信登录
     *
     * @param dto
     * @return
     */
    @GetMapping("/mobile_login")
    public CommonResult phoneLogin(@Valid MobileLoginDTO dto) {

        return CommonResult.ok("登录成功", loginService.login(dto));
    }


    /**
     * 邮箱+短信登录
     *
     * @param dto
     * @return
     */
    @GetMapping("/email_login")
    public CommonResult emailLogin(@Valid EmailLoginDTO dto) {

        return CommonResult.ok("登录成功", loginService.login(dto));
    }


    /**
     * 发送手机验证码
     *
     * @return
     */
    @GetMapping("/send_mobile_code")
    public CommonResult sendMobileCode(@Valid MobileSendVerifyCodeDTO codeDTO) {
        boolean sent = loginService.sendVerifyCode(codeDTO);
        return sent ? CommonResult.ok("发送短信成功") : CommonResult.error("手机号不正确或绑定多个登录账号，请校验手机号");
    }

    /**
     * 发送邮箱验证码
     *
     * @return
     */
    @GetMapping("/send_email_code")
    public CommonResult sendEmailCode(@Valid EmailSendVerifyCodeDTO codeDTO) {
        boolean sent = loginService.sendVerifyCode(codeDTO);
        return sent ? CommonResult.ok("发送邮件成功") : CommonResult.error("邮箱不正确或未注册，请校验邮箱");
    }

    /**
     * 找回密码
     *
     * @return
     */
    @GetMapping("/retrieve_password")
    public CommonResult retrievePassWord(@Valid MobileLoginDTO dto) {
        return CommonResult.ok("新密码已发送手机", loginService.retrievePassWord(dto));
    }
}
